package cn.outmaneuver.outman.demo.dynamic.database.config;
/**
 * Copyright 2023-2023 by dym. All Rights Reserved.
 */

import lombok.Data;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * DynamicDatabaseConfig
 * <p> </p>
 *
 * @author dym
 * @version 1.0
 * @date 2023/11/03
 */
@Configuration
public class DynamicDatabaseConfig {
    public final static Map<String, DataSourceConfig> DATA_SOURCE_CONFIG_MAP = new HashMap<>();

    {

        DATA_SOURCE_CONFIG_MAP.put("41999A", new DataSourceConfig("com.mysql.cj.jdbc.Driver", "jdbc:mysql://proxy" +
                ".outmaneuver.cn:3306/develop?characterEncoding=UTF-8&serverTimezone=UTC", "develop", "develop"));
        DATA_SOURCE_CONFIG_MAP.put("41999B", new DataSourceConfig("com.mysql.cj.jdbc.Driver", "jdbc:mysql://proxy" +
                ".outmaneuver.cn:3306/ganshu?characterEncoding=UTF-8&serverTimezone=UTC", "ganshu", "ganshu"));
    }

    @Data
    class DataSourceConfig {
        private String driverClassName;
        private String url;
        private String username;
        private String password;

        public DataSourceConfig(String driverClassName, String url, String username, String password) {
            this.driverClassName = driverClassName;
            this.url = url;
            this.username = username;
            this.password = password;
        }
    }

    /**
     * 默认数据源
     *
     * @return
     */
    @Bean(name = "defaultDataSource")
    @Qualifier("defaultDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.default")
    public DataSource defaultDataSource() {
        return DataSourceBuilder.create().build();
    }

    public DataSource newDataSource(DataSourceConfig dataSourceConfig) {
        return DataSourceBuilder.create().driverClassName(dataSourceConfig.driverClassName).url(dataSourceConfig.url).username(dataSourceConfig.username).password(dataSourceConfig.password).build();
    }


    @Bean(name = "dynamicDataSource")
    @Primary
    public DataSource dynamicDataSource() {
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        //保存我们有的数据源，方便后面动态增加
        dynamicDataSource.myMap = new HashMap<>();
//        dynamicDataSource.myMap.put("defaultDataSource", defaultDataSource());
        DATA_SOURCE_CONFIG_MAP.forEach((s, dataSourceConfig) -> {
            dynamicDataSource.myMap.put(s, newDataSource(dataSourceConfig));
        });
        dynamicDataSource.setTargetDataSources(dynamicDataSource.myMap);
        DynamicDataSourceContextHolder.dataSourceIds.addAll(dynamicDataSource.myMap.keySet());
        dynamicDataSource.setDefaultTargetDataSource(defaultDataSource());
        return dynamicDataSource;
    }
}
